/**
 * 
 */
package com.google.code.jkippt.http;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.client.utils.URIBuilder;

import com.google.code.jkippt.resource.Resource;

/**
 * @author "Federico De Faveri defaveri@gmail.com"
 *
 */
public class ResourceEndpointUrlFactory {
	
	public static final String SERVER_BASE_URL = "https://kippt.com";
	
	public static final char PATH_SEPARATOR = '/';
	public static final List<String> EMPTY_PATH = Collections.emptyList();
	public static final Map<String, String> NO_PARAMETERS = Collections.emptyMap();
	
	/**
	 * Returns the next page endpoint.
	 * @param next
	 * @return the next page endpoint url.
	 */
	public static String getNextPageEndpointUrl(String next)
	{
		return SERVER_BASE_URL + next;
	}

	public String endpoint;
	
	public ResourceEndpointUrlFactory(String endpointName)
	{
		this.endpoint = SERVER_BASE_URL + PATH_SEPARATOR + "api" + PATH_SEPARATOR + endpointName + PATH_SEPARATOR;
	}
	
	/**
	 * Returns the {@link Resource} endpoint.
	 * @param pathElements the path elements.
	 * @return the resulting endpoint url.
	 */
	public String buildEndpointUrl(String ... pathElements)
	{
		if (pathElements.length == 0) return endpoint;
		return buildEndpointUrl(Arrays.asList(pathElements), NO_PARAMETERS);
	}
	
	/**
	 * Returns the {@link Resource} endpoint for the specified parameters.
	 * @param parameters the endpointurl url parameters.
	 * @return the resulting endpoint url.
	 */
	public String buildEndpointUrl(Map<String, String> parameters)
	{
		return buildEndpointUrl(EMPTY_PATH, parameters);
	}
	
	/**
	 * Returns the {@link Resource} endpoint for the specified parameters.
	 * @param pathElement a single path element.
	 * @param parameters the endpoint url parameters.
	 * @return the resulting endpoint url.
	 */
	public String buildEndpointUrl(String pathElement, Map<String, String> parameters)
	{
		return buildEndpointUrl(Collections.singletonList(pathElement), parameters);
	}
	
	/**
	 * Returns the {@link Resource} endpoint for the specified parameters.
	 * @param pathElements the path elements.
	 * @param parameters the url parameters.
	 * @return the resulting endpoint url.
	 */
	public String buildEndpointUrl(List<String> pathElements, Map<String, String> parameters)
	{
		try {
			
			String baseUrl = endpoint;
			if (pathElements.size()>0) {
				StringBuilder baseurlBuilder = new StringBuilder(baseUrl);
				for (String pathElement:pathElements) {
					baseurlBuilder.append(pathElement);
					baseurlBuilder.append('/');
				}
				baseUrl = baseurlBuilder.toString();
			}
		
			URIBuilder builder = new URIBuilder(baseUrl);
			
			for (Entry<String, String> parameter:parameters.entrySet()) builder.addParameter(parameter.getKey(), parameter.getValue());
			
			return builder.build().toString();
		} catch (Exception e)
		{
			throw new IllegalArgumentException("Invalid parameters", e);
		}
	}

}
